package cn.haoxsf.app.system.service.impl;

import cn.haoxsf.app.common.api.enums.SystemExceptionEnum;
import cn.haoxsf.app.common.api.enums.UserExceptionEnum;
import cn.haoxsf.app.common.exception.BaseException;

import cn.haoxsf.app.common.util.JwtTokenUtil;
import cn.haoxsf.app.common.util.ToolUtil;

import cn.haoxsf.app.system.dto.LoginUser;
import cn.haoxsf.app.system.dto.MemberDetailsService;
import cn.haoxsf.app.system.entity.Member;
import cn.haoxsf.app.system.service.IAuthService;
import cn.haoxsf.app.system.service.IMemberService;

import cn.haoxsf.app.system.service.IRedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @program: app
 * @description: 用户鉴权实现
 * @author: Ele qvbanfm@qq.com
 * @create: 2020-01-07 09:27
 **/
@Service
@Log4j2
public class AuthServiceImpl implements IAuthService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IRedisService redisService;

    @Autowired
    private MemberDetailsService detailsService;

    /**
     * 登陆
     *
     * @param userName 用户名
     * @param pwd      密码
     * @return token
     */
    @Override
    public String login(String userName, String pwd) {
        LoginUser loginUser = detailsService.loadUserByUsername(userName);
        if (ToolUtil.isEmpty(loginUser)) {
            throw new BaseException(UserExceptionEnum.NOT_USERS);
        }
        boolean matches = passwordEncoder.matches(pwd, loginUser.getPassword());
        if (!matches) {
            throw new BaseException(UserExceptionEnum.PASSWORD_NOT);
        }
        String string = redisService.getString(loginUser.getId().toString() + ":" + loginUser.getUsername());
        if (ToolUtil.isNotEmpty(string)) {
            return string;
        }
        String token = jwtTokenUtil.generateToken(loginUser);
        //绑定用户
        redisService.setString(loginUser.getId().toString() + ":" + loginUser.getUsername(), token);
        return token;
    }

    /**
     * 微信一键登陆
     *
     * @param userName
     * @return
     */
    @Override
    public String authLogin(String userName) {
        LoginUser loginUser = detailsService.loadUserByUsername(userName);
        if (ToolUtil.isEmpty(loginUser)) {
            throw new BaseException(UserExceptionEnum.NOT_USERS);
        }
        String string = redisService.getString(loginUser.getId().toString() + ":" + loginUser.getUsername());
        if (ToolUtil.isNotEmpty(string)) {
            return string;
        }
        String token = jwtTokenUtil.generateToken(loginUser);
        //绑定用户
        redisService.setString(loginUser.getId().toString() + ":" + loginUser.getUsername(), token);
        return token;
    }

    /**
     * 注销
     */
    @Override
    public void logout() {
        LoginUser loginUser = this.getLoginUser();
        redisService.logout(loginUser.getId() + ":" + loginUser.getUsername());
    }


    /**
     * 是否已登陆
     *
     * @param username 用户名
     * @return 已登陆为 true
     */
    @Override
    public Boolean isLogin(String username) {
        String loginUsername = getLoginUser().getUsername();
        return username.equals(loginUsername);
    }

    @Override
    public LoginUser getLoginUser() {
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String) {
            throw new BaseException(SystemExceptionEnum.UNAUTHORIZED);
        }
        return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
